package lkaixuan;

import java.util.*;

public class DoubleColorBall {
    public static void main(String[] args) {
        //生成随机的红球号码
        Set<Integer> redBalls = new HashSet<>();
        Random random = new Random();

        while (redBalls.size() < 6) {
            int num = random.nextInt(33) + 1;
            redBalls.add(num);
        }

        System.out.println("生成的随机红球号码为：" + redBalls);

        //生成随机的蓝球数量
        int blueBall = random.nextInt(16) + 1;
        System.out.println("生成的随机蓝球号码为：" + blueBall);

        // 用户输入的红球号码
        Scanner scanner = new Scanner(System.in);
        List<Integer> userRedBalls = new ArrayList<>();
        for (int i = 1; i <= 6; i++) {
            boolean validInput = false;
            while (!validInput) {
                System.out.println("请输入第 " + i + " 个红球号码（1 - 33）：");
                int num = scanner.nextInt();
                if (num < 1 || num > 33) {
                    System.out.println("号码超出范围，请重新输入：");
                } else {
                    userRedBalls.add(num);
                    validInput = true;
                }
            }
        }
        //用户输入的蓝球号码
        int blueNumber = 0;
        while (true) {
            System.out.println("请输入蓝球号码，范围 1 - 16：");
            blueNumber = scanner.nextInt();
            if (blueNumber >= 1 && blueNumber <= 16) {
                break;
            } else {
                System.out.println("输入无效，请输入一个 1 - 16 的数字。");
            }
        }
        //中奖红球号码
        List<Integer> winRedNumbers = new ArrayList<>();
        while (winRedNumbers.size() < 6) {
            int redNumber = random.nextInt(33) + 1;
            if (!winRedNumbers.contains(redNumber)) {
                winRedNumbers.add(redNumber);
            }
        }
        //中奖蓝球号码
        int winBlueNumber = random.nextInt(16) + 1;


        int redMatchCount = 0;
        for (int num :redBalls) {
            if (winRedNumbers.contains(num)) {
                redMatchCount++;
            }
        }
        int blueMatch = (blueNumber == winBlueNumber) ? 1 : 0;
        //确定中奖级别
        String prizeLevel;
        if (redMatchCount == 6 && blueMatch ==1)
        {
            prizeLevel ="一等奖";
        }
        else if (redMatchCount == 6)
        {
            prizeLevel = "二等奖";
        }
        else if (redMatchCount == 5 && blueMatch ==1)
        {
            prizeLevel = "三等奖";
        }
        else if (redMatchCount ==5 ||(redMatchCount ==4 && blueMatch == 1))
        {
            prizeLevel = "四等奖";
        }
        else if (redMatchCount == 4 || (redMatchCount == 3 && blueMatch == 1))
        {
            prizeLevel = "五等奖";
        }
        else if (redMatchCount == 2 && blueMatch == 1|| redMatchCount == 1 && blueMatch == 1 || blueMatch == 1)
        {
            prizeLevel = "六等奖";
        }
        else
        {
            prizeLevel = "未中奖";
        }
        System.out.println("您命中了 " + (redMatchCount + blueMatch) + " 个号码，中奖级别为：" + prizeLevel);
    }
}

